package com.limit.aspect;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

/**
 * @title: ControllerAdvice
 * @Author xu-xiang
 * @Date: 2024/1/30 17:02
 * @description:
 */
@ControllerAdvice
public class WebExceptionHandler {

    /**
     * 兜底异常
     */
    @ExceptionHandler(Throwable.class)
    public ModelAndView baseExceptionHandler(Throwable e) {
        MappingJackson2JsonView view = new MappingJackson2JsonView();
        view.setExtractValueFromSingleKeyModel(true);
        ModelAndView modelAndView = new ModelAndView(view);
        modelAndView.addObject("请求出错,原因: " + e.getMessage());
        return modelAndView;
    }
}
